﻿import QtQuick 2.0
import "../components"


// 目前处理的内容是阀门的按钮
Item {
    id: root
    property int valveNum: 15
    height: 200*dp;  width: 200*dp
    property bool editable: expStatus===0 && gConnectionState
//    property bool editable: true

    Repeater{       // 阀门坐标
        model: valveNum
        Item{
            id: item
            property bool isPressed: false
            x: root.width * ncPosi.valve[index+1]["x"] + width * ncPosi.valveLabel[index+1]["x"] - 0.5*width
            y: root.height * ncPosi.valve[index+1]["y"] + height * ncPosi.valveLabel[index+1]["y"] - 0.5*width
            height: 20*dp + ((root.height > 500*dp ? (root.height-500*dp)*0.01 : 0 ))
            width: 28*dp + ((root.width > 700*dp ? (root.width-700*dp)*0.01 : 0 ))
            Image{      // 按钮
                anchors.fill: parent
                source: {

                    if(isPressed){
                        if(valveAngle[index]>0.25){
                            return "qrc:/picture/valveButton/bcg_g0.png"
                        }else{
                            return "qrc:/picture/valveButton/bcg_r0.png"
                        }
                    }else{
                        if(valveAngle[index]>0.25){
                            return "qrc:/picture/valveButton/bcg_g1.png"
                        }else{
                            return "qrc:/picture/valveButton/bcg_r1.png"
                        }
                    }
                }
            }
            Image{      // 字
                anchors.fill: parent
                source: "qrc:/picture/valveButton/v"+(index+1)+".png"
//                source: valveAngle[index]===0.5 ? "qrc:/picture/valveButton/v"+(index+1)+"-o.png" : "qrc:/picture/valveButton/v"+(index+1)+"-c.png"
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                cursorShape: editable ? Qt.PointingHandCursor :Qt.ForbiddenCursor
                onClicked: {
                    if(!editable){
                        return
                    }

//                    // 判断阀门是开着的还是关着的，给后端发指令
//                    var openList = []
//                    var closeList = []
//                    for(let i = 0; i < 15; i ++){
//                        if(i === index){
//                            if(valveAngle[i]===0.5){
//                                closeList.push(i+1)
//                            }else{
//                                openList.push(i+1)
//                            }
//                        }else{
//                            if(valveAngle[i]===0.5){
//                                openList.push(i+1)
//                            }else{
//                                closeList.push(i+1)
//                            }
//                        }
//                    }
////                    console.log("打开阀门列表", openList)
////                    console.log("关闭阀门列表", closeList)
//                    CommunicationMethod.stepOperatingSimulation("2", [openList.toString(), closeList.toString()])
                    CommunicationMethod.stepOperatingSimulation("2", [valveAngle[index]===0.5?"关闭":"开启", (index+1).toString()])
                    CAnimation.changeValve(index, valveAngle[index]===0.5 ? false : true)
                }
                onPressed: {
                    if(!editable){
                        return
                    }
                    item.isPressed = true
                }onReleased: {
                    if(!editable){
                        return
                    }
                    item.isPressed = false
                }
            }

        }

    }

    // 临时测试用，设置管道流速
    Item{
        height: 20*dp; width: 20*dp
        x: 10*dp; y: 80*dp
        Text {
            id: text__
            text: "->"
            color: "#AAA"
            font.pixelSize: 20*dp
        }
        MouseArea{
            hoverEnabled: true
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onEntered: {
                text__.color = "#CCCCCC"
            }onExited: {
                text__.color = "#AAAAAA"
            }
            onClicked: {
                column_pipe.visible = !column_pipe.visible
            }
        }
    }

    Column{
        id: column_pipe
        opacity: 0.3
        visible: false
        height: 200*dp; width: 200*dp
        y: 100*dp; x:10*dp
        CustomInputText{
            id: edit_pipe
            title: "内径"
            titleWidth: 35*dp; editWidth: 60*dp
            defaultText: "0.6"
            douflag: true
            height: 30*dp
        }PushButton{
            title: "确定(测试用)"
            defaultMode1: true
            height: 30*dp
            width: 95*dp
            onReleased: {
                if(Number(edit_pipe.getText()) < 0.1){
                    edit_pipe.setText("0.1")
                }else if(Number(edit_pipe.getText()) > 5){
                    edit_pipe.setText("5")
                }
                CAnimation.changePipeDiameter(Number(edit_pipe.getText()))
                text_pipe.text = "[1ml/s]->[" + (4/3.1415926/Number(edit_pipe.getText())/Number(edit_pipe.getText())).toFixed(2) + "mm/ms]"
            }
        }Text{
            id: text_pipe
            font.pixelSize: config.fontsize - 4*dp
            text: "[1ml/s]->[3.54mm/ms]"
        }
    }

}
